﻿# CoffeeScript
Moshu = window.Moshu
tests = new Moshu.Tests("")
tests.add("Vector", ()->
        v1 = new Moshu.Canvas2D.Vector(15,33)        
        tests.assert("Constructor test - ", v1.x() is 15 and v1.y() is 33)
                
        v2 = new Moshu.Canvas2D.Vector(75,12)
        
        r = v1.substract(v2)
        tests.assert("Substraction test - ", r.x() is -60 and r.y() is 21)
        
        r = v1.add(v2)
        tests.assert("Addition test - ", r.x() is 90 and r.y() is 45)
        
        r = v1.dotProduct(v2)        
        tests.assert("Dot product test - ", r is 1521)
        
        r = v1.getPerpendicular()
        tests.assert("Get perpendicular test - ", r.x() is 33 and r.y() is -15)
        
        r = v1.getMagnitude()
        tests.assert("Get magnitude test - ", r is 36.24913792078372)
    )

tests.add("PhysicalObject", () ->
        o = new Moshu.Canvas2D.PhysicalObject()
        v1 = new Moshu.Canvas2D.Vector(15,33)   
        
        # test velocity on fixed object
        o.fixed = true
        o.velocity(v1)
        r = o.velocity()
        tests.assert("Set velocity of fixed obj",r.x() is 0 and r.y() is 0)
        
        # test velocity on mobile object
        o.fixed = false
        o.velocity(v1)
        r = o.velocity()
        tests.assert("Set velocity of mobile obj",r.x() is 15 and r.y() is 33)
        
        # test momentum
        o.fixed = false
        o.velocity(v1)
        o.mass = 2
        r = o.getMomentum()
        tests.assert("GetMomentum ",r.x() is 30 and r.y() is 66)
    )
    
tests.add("Polygon", ()->
        # build a triangle for testing
        vertices = [
            new Moshu.Canvas2D.Vector(0,0),
            new Moshu.Canvas2D.Vector(0,100),
            new Moshu.Canvas2D.Vector(100,100),
        ]
        
        # constructor test
        o = new Moshu.Canvas2D.Polygon(vertices)
        tests.assert("Constructor test", o._vertices.length is 3 and o._vertices[0].x() is 0 and o._vertices[0].y() is 0 and o._vertices[1].x() is 0 and o._vertices[1].y() is 100 and o._vertices[2].x() is 100 and o._vertices[2].y() is 100)
        
        # move test
        o.move(new Moshu.Canvas2D.Vector(100,100))        
        tests.assert("Move test", o._vertices.length is 3 and o._vertices[0].x() is 100 and o._vertices[0].y() is 100 and o._vertices[1].x() is 100 and o._vertices[1].y() is 200 and o._vertices[2].x() is 200 and o._vertices[2].y() is 200)
        
        # get side vectors
        o = new Moshu.Canvas2D.Polygon(vertices)
        sides = o.getSidesVectors()
        # for simplicity we only test the first           
        tests.assert("getSidesVectors",sides[0].x() is 0 and sides[0].y() is -100)
        
        #test projection
        axis = sides[2];
        axis = axis.getPerpendicular();
        
        projection = o.getProjection(axis);        
        tests.assert("getProjection",projection[0] is -10000 and projection[1] is 0)
        
        #test collision true
        o1 = new Moshu.Canvas2D.Polygon([
            new Moshu.Canvas2D.Vector(0,80),
            new Moshu.Canvas2D.Vector(188,180)
        ]
        )
        r = o.getCollision(o1)        
        tests.assert("getCollision - colliding",r)
        
        o1 = new Moshu.Canvas2D.Polygon([
            new Moshu.Canvas2D.Vector(0,102),
            new Moshu.Canvas2D.Vector(190,190)
        ]
        )
        r = o.getCollision(o1)
        tests.assert("getCollision - not colliding",not r)
        
        o1.move(new Moshu.Canvas2D.Vector(5,-10))
        r = o.getCollision(o1)
        tests.assert("getCollision - colliding after move",r)
        
        o1.move(new Moshu.Canvas2D.Vector(5,10))
        r = o.getCollision(o1)
        tests.assert("getCollision - not colliding after move",not r)
    )
    
tests.test()

